package com.fuxi.cloud.desreport.a;

import com.fuxi.cloud.common.util.JimuReportRepository;
import com.fuxi.cloud.common.vo.Result;
import com.fuxi.cloud.config.client.JmReportTokenClient;
import com.fuxi.cloud.desreport.entity.JimuReportDictItem;
import com.fuxi.cloud.desreport.model.JmPage;
import com.fuxi.cloud.desreport.service.IJimuReportDictItemService;
import com.fuxi.cloud.desreport.service.IJimuReportDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

/**
 * b
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
@RestController("jimuReportDictItemController")
@RequestMapping({"com.fuxi.cloud/dictItem"})
public class c {
    @Autowired
    private IJimuReportDictItemService dictItemService;
    @Autowired
    private JmReportTokenClient jimuTokenClient;
    @Autowired
    private IJimuReportDictService dictService;

    public c() {
    }

    @RequestMapping(
            value = {"/list"},
            method = {RequestMethod.GET}
    )
    public Result<JmPage<JimuReportDictItem>> a(JimuReportDictItem var1, @RequestParam(name = "pageNo",defaultValue = "1") Integer var2, @RequestParam(name = "pageSize",defaultValue = "10") Integer var3, HttpServletRequest var4) {
        Result var5 = new Result();
        JmPage var6 = this.dictItemService.queryPageList(var1, var2, var3);
        var5.setSuccess(true);
        var5.setResult(var6);
        return var5;
    }

    @RequestMapping(
            value = {"/add"},
            method = {RequestMethod.POST}
    )
    @CacheEvict(
            value = {"jmreport:cache:dict", "jmreport:cache:dictTxt"},
            allEntries = true
    )
    public Result<JimuReportDictItem> a(@RequestBody JimuReportDictItem var1, HttpServletRequest var2) {
        Result var3 = new Result();
        this.jimuTokenClient.getUsername(var2);
        if (!JimuReportRepository.getSaas()) {
            String var4 = "";
        }

        Boolean var5 = this.a(var1);
        if (!var5) {
            var3.setSuccess(false);
            var3.setMessage("数据值已重复");
            return var3;
        } else {
            try {
                var1.setCreateTime(new Date());
                this.dictItemService.save(var1);
                var3.success("保存成功！");
            } catch (Exception var7) {
                var3.error500("操作失败");
            }

            return var3;
        }
    }

    @RequestMapping(
            value = {"/edit"},
            method = {RequestMethod.POST}
    )
    @CacheEvict(
            value = {"jmreport:cache:dict", "jmreport:cache:dictTxt"},
            allEntries = true
    )
    public Result<JimuReportDictItem> b(@RequestBody JimuReportDictItem var1, HttpServletRequest var2) {
        Result var3 = new Result();
        Boolean var4 = this.a(var1);
        this.jimuTokenClient.getUsername(var2);
        if (!JimuReportRepository.getSaas()) {
            String var5 = "";
        }

        if (!var4) {
            var3.setSuccess(false);
            var3.setMessage("数据值已重复");
            return var3;
        } else {
            JimuReportDictItem var6 = this.dictItemService.getById(var1.getId());
            if (var6 == null) {
                var3.error500("未找到对应实体");
            } else {
                var1.setUpdateTime(new Date());
                int var7 = this.dictItemService.updateById(var1);
                if (var7 > 0) {
                    var3.success("编辑成功!");
                }
            }

            return var3;
        }
    }

    @RequestMapping(
            value = {"/delete"},
            method = {RequestMethod.DELETE}
    )
    @CacheEvict(
            value = {"jmreport:cache:dict", "jmreport:cache:dictTxt"},
            allEntries = true
    )
    public Result<JimuReportDictItem> a(@RequestParam(name = "id") String var1, HttpServletRequest var2) {
        Result var3 = new Result();
        this.jimuTokenClient.getUsername(var2);
        if (!JimuReportRepository.getSaas()) {
            String var4 = "";
        }

        JimuReportDictItem var5 = this.dictItemService.getById(var1);
        String var6 = var5.getDictId();
        if (var5 == null) {
            var3.error500("未找到对应实体");
        } else {
            Integer var7 = this.dictItemService.removeById(var1);
            if (var7 > 0) {
                var3.success("删除成功!");
            }
        }

        return var3;
    }

    @RequestMapping(
            value = {"/deleteBatch"},
            method = {RequestMethod.DELETE}
    )
    @CacheEvict(
            value = {"jmreport:cache:dict", "jmreport:cache:dictTxt"},
            allEntries = true
    )
    public Result<JimuReportDictItem> a(@RequestParam(name = "ids") String var1) {
        Result var2 = new Result();
        if (var1 != null && !"".equals(var1.trim())) {
            this.dictItemService.removeByIds(Arrays.asList(var1.split(",")));
            var2.success("删除成功!");
        } else {
            var2.error500("参数不识别！");
        }

        return var2;
    }

    public Boolean a(JimuReportDictItem var1) {
        boolean var2 = false;
        int var3 = this.dictItemService.count(var1);
        return var3 == 0 ? true : false;
    }
}
